package org.dbview.controller;

import java.util.List;
import java.util.Map;

import org.dbview.model.SimpleResponse;
import org.dbview.model.UserIndColumns;
import org.dbview.model.ViewDictionary;
import org.dbview.service.ITableInfoService;
import org.dbview.service.IViewDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TableInfoController {
	
	@Autowired
	private IViewDictionaryService viewDictionaryService;
	
	@Autowired
	private ITableInfoService tableInfoService;
	
	@RequestMapping(value = "updateTableComment.do")
	@ResponseBody
	public SimpleResponse updateTableComment(
			@RequestBody Map<String, String> paramsMap) {
		tableInfoService.updateTableComment(paramsMap.get("tableName"),
				paramsMap.get("comment"));
		List<ViewDictionary> list = viewDictionaryService
				.findViewByTable(paramsMap.get("tableName"));
		if (list != null && !list.isEmpty()) {
			ViewDictionary vd = list.get(0);
			if (paramsMap.get("comment").equals(vd.getTableComment())) {
				return new SimpleResponse(Boolean.TRUE);
			}
		}
		return new SimpleResponse(Boolean.FALSE, "修改表注解失败");
	}
	
	@RequestMapping(value = "updateColumnComment.do")
	@ResponseBody
	public SimpleResponse updateColumnComment(
			@RequestBody Map<String, String> paramsMap) {
		tableInfoService.updateColumnComment(paramsMap.get("tableName"),
				paramsMap.get("columnName"), paramsMap.get("comment"));
		List<ViewDictionary> list = viewDictionaryService
				.findViewByTableAndColumn(paramsMap.get("tableName"),
						paramsMap.get("columnName"));
		if (list != null && !list.isEmpty()) {
			ViewDictionary vd = list.get(0);
			if (paramsMap.get("comment").equals(vd.getColComment())) {
				return new SimpleResponse(Boolean.TRUE);
			}
		}
		return new SimpleResponse(Boolean.FALSE, "修改字段注解失败");
	}

	@RequestMapping(value = "findIndexByTable.do")
	@ResponseBody
	public List<UserIndColumns> findIndexByTable(@RequestBody String tableName) {
		return tableInfoService.findIndexByTable(tableName);
	}
}
